package fms.core;

import fms.acarsMsgProcessUtil.DownLinkDatagramHeader;
import fms.msgupcenter.protocols.AcarsProtocol;
import fms.msgupcenter.protocols.uplinkmsg.*;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class WindXmlBuilder {
    private Logger logger = Logger.getLogger(WindXmlBuilder.class);

    public String build(String aContent, String upMsg, String replySMI) {
        logger.debug("[xml构造] 开始进行上传xml构造...");
        DownLinkDatagramHeader header = null;
        try {
            header = DownLinkDatagramHeader.Parse(aContent);
        } catch (Exception e) {
            logger.error("[xml构造] 解析报文头出错!" + e.getMessage());
            return generateBlankUplinkMsg();
        }

        UplinkMsgProtocol uplinkMsgProtocol = new UplinkMsgProtocol();
        UplinkMsgProtocolBody body = uplinkMsgProtocol.body;
        UplinkMessage uplinkMessage = body.uplinkMessage;
        UplinkMsgHead uplinkMsgHead = new UplinkMsgHead();
        String appUpMsgId=Long.toString(new Date().getTime());
        appUpMsgId=appUpMsgId.substring(appUpMsgId.length()-10);
//        uplinkMsgHead.setAppUpMsgId(UUID.randomUUID().toString());
        //机尾号需要在字母和数字间增加"-"
        if(!header.getAn().contains("-")){
            uplinkMsgHead.setAn(header.getAn().substring(0,1)+"-"+header.getAn().substring(1));
        }
        else{
            uplinkMsgHead.setAn(header.getAn());
        }
        uplinkMsgHead.setAirspace(true);
        uplinkMsgHead.setRcvAddress("Receive");
        uplinkMsgHead.setSndAddress("Send");
        uplinkMsgHead.setSmi(replySMI);
        uplinkMsgHead.setMa(true);
        uplinkMsgHead.setAppUpMsgId(appUpMsgId);
        UplinkMessageData uplinkMessageData = new UplinkMessageData(upMsg);
//        UplinkMessageData uplinkMessageData = new UplinkMessageData(upMsg+"/SN"+appUpMsgId);
        uplinkMessage.setHead(uplinkMsgHead);
        uplinkMessage.setData(uplinkMessageData);
        uplinkMessage.setMsg("");

        try {
            logger.debug("执行AcarsProtocol.Serialize(uplinkMsgProtocol)序列化！");
            return AcarsProtocol.Serialize(uplinkMsgProtocol);
        } catch (Exception e) {
            logger.error("[xml构造] 序列化出错!" + e.getMessage());
        }
        return "";
    }

    private String generateBlankUplinkMsg() {
        UplinkMsgProtocol uplinkMsgProtocol = new UplinkMsgProtocol();
        UplinkMsgProtocolBody body = uplinkMsgProtocol.body;
        UplinkMessage uplinkMessage = body.uplinkMessage;
        UplinkMsgHead uplinkMsgHead = new UplinkMsgHead();
        uplinkMsgHead.setAn("");
        uplinkMsgHead.setSmi("");
        uplinkMsgHead.setAirspace(true);
        uplinkMsgHead.setRcvAddress("");
        uplinkMsgHead.setSndAddress("");
        uplinkMsgHead.setMa(true);

        UplinkMessageData uplinkMessageData = new UplinkMessageData("");
        uplinkMessageData.setText("");

        uplinkMessage.setHead(uplinkMsgHead);
        uplinkMessage.setData(uplinkMessageData);

        try {
            logger.info("执行AcarsProtocol.Serialize(uplinkMsgProtocol)序列化！");
            return AcarsProtocol.Serialize(uplinkMsgProtocol);
        } catch (Exception e) {
            logger.error("[xml构造] 序列化出错!" + e.getMessage());
        }
        return "";
    }


//    public String build(String aContent, String upMsg, String replySMI) {
//        logger.debug("[xml构造] 开始进行上传xml构造...");
//        DownLinkDatagramHeader header = null;
//        try {
//            header = DownLinkDatagramHeader.Parse(aContent);
//        } catch (Exception e) {
//            logger.error("[xml构造] 解析报文头出错!" + e.getMessage());
//            return generateBlankUplinkMsg();
//        }
//
//        UplinkMsgProtocol uplinkMsgProtocol = new UplinkMsgProtocol();
//        UplinkMsgProtocolBody body = uplinkMsgProtocol.body;
//        UplinkMessage uplinkMessage = body.uplinkMessage;
//
//        UplinkMsgHead uplinkMsgHead = new UplinkMsgHead();
//        uplinkMsgHead.setAppUpMsgId(UUID.randomUUID().toString());
//        uplinkMsgHead.setAppUpMsgType("UPLINKFMS");
//        //机尾号需要在字母和数字间增加"-"
//        if(header.getAn().startsWith("B")){
//            uplinkMsgHead.setAn(header.getAn().substring(0,1)+"-"+header.getAn().substring(1));
//        }
//        else{
//            uplinkMsgHead.setAn(header.getAn());
//        }
//        uplinkMsgHead.setFi(header.getFi().toString());
//        uplinkMsgHead.setOriginAirport("");
//        uplinkMsgHead.setDestAirport("");
//        uplinkMsgHead.setSmi(replySMI);
//        uplinkMsgHead.setAutoreply(true);
//        uplinkMsgHead.setApptype("FMS");
//        uplinkMsgHead.setAirline(header.getFi().toString().substring(0,2));
//        uplinkMsgHead.setOnlyLastReply(true);
//        uplinkMsgHead.setTag("");
//
//        SendRules sendRules = new SendRules();
//        List<Sender> senders = new ArrayList<Sender>();
//        Sender sender = new Sender(SendRules.SenderType.Default, "", "", 0, 0);
//        senders.add(sender);
//        sendRules.setAllowMaxTime(600);
//        sendRules.setSenders(senders);
//
//        UplinkMessageData uplinkMessageData = new UplinkMessageData(upMsg);
//
//        uplinkMessage.setHead(uplinkMsgHead);
//        uplinkMessage.setData(uplinkMessageData);
//        uplinkMessage.setSendRules(sendRules);
//        uplinkMessage.setMsg("");
//        try {
//            logger.debug("执行AcarsProtocol.Serialize(uplinkMsgProtocol)序列化");
//            return AcarsProtocol.Serialize(uplinkMsgProtocol);
//        } catch (Exception e) {
//            logger.error("[xml构造] 序列化出错!" + e.getMessage());
//        }
//        return "";
//    }
//
//    private String generateBlankUplinkMsg() {
//        UplinkMsgProtocol uplinkMsgProtocol = new UplinkMsgProtocol();
//        UplinkMsgProtocolBody body = uplinkMsgProtocol.body;
//        UplinkMessage uplinkMessage = body.uplinkMessage;
//        UplinkMsgHead uplinkMsgHead = new UplinkMsgHead();
//        uplinkMsgHead.setAppUpMsgId(UUID.randomUUID().toString());
//        uplinkMsgHead.setAppUpMsgType("UPLINKFMS");
//        uplinkMsgHead.setAn("");
//        uplinkMsgHead.setFi("");
//        uplinkMsgHead.setOriginAirport("");
//        uplinkMsgHead.setDestAirport("");
//        uplinkMsgHead.setSmi("");
//        uplinkMsgHead.setAutoreply(true);
//        uplinkMsgHead.setApptype("FMS");
//        uplinkMsgHead.setAirline("");
//        uplinkMsgHead.setOnlyLastReply(true);
//
//        SendRules sendRules = new SendRules();
//        List<Sender> senders = new ArrayList<Sender>();
//        Sender sender = new Sender(SendRules.SenderType.Default, "", "", 0, 0);
//        senders.add(sender);
//        sendRules.setAllowMaxTime(600);
//        sendRules.setSenders(senders);
//
//        UplinkMessageData uplinkMessageData = new UplinkMessageData("");
//        uplinkMessageData.setText("");
//
//        uplinkMessage.setHead(uplinkMsgHead);
//        uplinkMessage.setData(uplinkMessageData);
//        uplinkMessage.setSendRules(sendRules);
//        try {
//            logger.info("执行AcarsProtocol.Serialize(uplinkMsgProtocol)序列化");
//            return AcarsProtocol.Serialize(uplinkMsgProtocol);
//        } catch (Exception e) {
//            logger.error("[xml构造] 序列化出错!" + e.getMessage());
//        }
//        return "";
//    }
}
